package com.shuqi.model.parse.parser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.shuqi.database.model.BookCataLog;
import com.shuqi.database.model.BookInfo;
import com.shuqi.model.bean.BookData;
import com.shuqi.model.parse.HandlerBase;

/**
 * 下载某书全部目录功能
 * 
 * @author hxp
 */
public class PayDownCatalogParser extends HandlerBase {
    private List<BookCataLog> infos;
    private BookData data;
    private String mBookId;
    private String mSourceId;
    private String mUid;
    private int mOid;
    private int mMaxoid;

    /**
     * 构造函数
     * 
     * @param data2
     */
    public PayDownCatalogParser(String bookId,String sourceId,String uId) {
        mBookId = bookId;
        if(sourceId == null){
            sourceId = "";
        }
        mSourceId = sourceId;
        mUid = uId;
        mOid = 1;
        mMaxoid = 0;
    }

    @Override
    public void startDocument() throws SAXException {
        data = new BookData();
        data.setBookId(mBookId);
        data.setSourceId(mSourceId);
        infos = data.getInfos();
        if (infos == null) {
            infos = new ArrayList<BookCataLog>();
            data.setInfos(infos);
        } else {
            infos.clear();
        }
        super.startDocument();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attrs)
            throws SAXException {
        if ("ChapterList".equals(localName)) {
            data.setBookName(isNull(attrs, "bookName"));
            data.setAuthorName(isNull(attrs,"author"));
            data.setCoverImg(isNull(attrs, "imgUrl"));
            String payMode = isNull(attrs,"payMode");
            if(payMode != null){
                try{
                    data.setPayMode(Integer.valueOf(payMode));
                }catch(Exception e){
                    data.setPayMode(BookInfo.FREE);
                }
            }else{
                data.setPayMode(BookInfo.FREE);
            }
            data.setHide(isNull(attrs, "hide"));
            String coverOpen = isNull(attrs, "cr_cover_isopen");
            if(coverOpen != null){
                try{
                    data.setCoverIsopen(Integer.valueOf(coverOpen)); 
                }catch(Exception e){
                    data.setCoverIsopen(BookInfo.OPEN);
                }
            }else{
                data.setCoverIsopen(BookInfo.OPEN);
            }
            String readOpen = isNull(attrs, "cr_read_isopen");
            if(readOpen != null){
                try{
                    data.setReadIsopen(Integer.valueOf(readOpen));
                }catch(Exception e){
                    data.setReadIsopen(BookInfo.OPEN);
                }
            }else{
                data.setReadIsopen(BookInfo.OPEN);
            }
            String maxOid = isNull(attrs,"maxOid");
            if(maxOid != null){
                try{
                    mMaxoid = Integer.valueOf(maxOid);
                    data.setOidMax(mMaxoid);
                }catch(Exception e){
                    
                }
            }
            data.setWordCount(isNull(attrs, "wordCount"));
            data.setChapterUpdateTime(System.currentTimeMillis());
        } else if ("Chapter".equals(localName)) {
            BookCataLog catalog = new BookCataLog();
            catalog.setBookId(mBookId);
            catalog.setSourceId(mSourceId);
            catalog.setUserId(mUid);
            infos.add(catalog);
            catalog.setChapterId(isNull(attrs, "chapterId"));
            catalog.setChapterName(isNull(attrs, "chapterName"));
            catalog.setVolumeId(isNull(attrs, "vid"));
            String isChapter = isNull(attrs, "isChapter");
            if(isChapter != null){
                try{
                    catalog.setChapterState(Integer.valueOf(isChapter));
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            String payMode = isNull(attrs, "payMode");
            if(payMode != null){
                try{
                    catalog.setPayMode(Integer.valueOf(payMode));
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            catalog.setChapterPrice(isNull(attrs, "price"));
            String paid = isNull(attrs, "paid");
            if(paid != null){
                try{
                    catalog.setPayState(Integer.valueOf(paid));
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            String wordCount = isNull(attrs, "wordCount");
            if(wordCount != null){
                try{
                    catalog.setChapterWordCount(Long.valueOf(wordCount));
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            catalog.setChapterContentUrl(isNull(attrs, "contentURL"));
            if(catalog.getChapterState() == BookCataLog.IS_CHATPTER){
                catalog.setOId(mOid++);
                if(mOid-1 > mMaxoid){
                    data.setOidMax(mOid-1);
                }
            }
        }
        super.startElement(uri, localName, qName, attrs);
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }

    @Override
    public BookData getParsedData() {
        return data;
    }
}
